﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

using DotNetNuke.Entities.Modules;
using DesertedRoadStudios.BlogNetNuke.Common;
using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke.UI.Widgets
{
    public class Categories : PortalModuleBase
    {
        public List<BlogCategory> BlogCategories { get { return _categories; } }

        private List<BlogCategory> _categories;
        private XDocument _settings;
        private Dictionary<string, string> _widgetSettings;
        private string _categoriesSortedBy;
        private string _categoriesSortOrder;
        private bool _categoriesShowPostCount;

        public class BlogCategory : DesertedRoadStudios.BlogNetNuke.Entities.Category.CategoryInfo
        {
            public int Count = 0;
        }

        public Categories()
        {
        }

        public Categories(XDocument settings, Dictionary<string, string> widgetSettings, IEnumerable<PostInfo> posts)
        {
            _settings = settings;
            _widgetSettings = widgetSettings;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            CheckWidget();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            RenderCategories();
        }

        private void CheckWidget()
        {
            if (!_widgetSettings["Widget"].Equals("Categories"))
                throw new System.Exception("Control doesn't implement DesertedRoadStudios.BlogNetNuke.UI.Widgets." + _widgetSettings["Widget"]);

            if (!_widgetSettings.ContainsKey("CategoriesSortedBy"))
                _categoriesSortedBy = "Category Name";
            else
                _categoriesSortedBy = _widgetSettings["CategoriesSortedBy"];

            if (!_widgetSettings.ContainsKey("CategoriesSortOrder"))
                _categoriesSortOrder = "Descending";
            else
                _categoriesSortOrder = _widgetSettings["CategoriesSortOrder"];

            if (!_widgetSettings.ContainsKey("CategoriesShowPostCount"))
                _categoriesShowPostCount = false;
            else
                _categoriesShowPostCount = Convert.ToBoolean(_widgetSettings["CategoriesShowPostCount"]);
        }

        private void RenderCategories()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            IEnumerable<PostsCategories> categories = bnn.GetAllCategoriesForPosts(Convert.ToInt32(_settings.Element("params").Element("Blog").Element("BlogDetails").Element("PortalID").Value), Convert.ToInt32(_settings.Element("params").Element("Blog").Element("BlogDetails").Element("ModuleID").Value));
            List<BlogCategory> tempCategories = new List<BlogCategory>();

            foreach (int categoryID in categories.Select(c => c.CategoryID).Distinct())
            {
                BlogCategory tempCategory = new BlogCategory();
                PostsCategories postCategory = categories.Where(c => c.CategoryID == categoryID).First();
                tempCategory.Category = postCategory.Category;
                tempCategory.CategoryID = postCategory.CategoryID;
                tempCategory.Description = postCategory.Description;
                tempCategory.Keywords = postCategory.Keywords;
                tempCategory.ModuleID = postCategory.ModuleID;
                tempCategory.ParentID = postCategory.ParentID;
                tempCategory.PortalID = postCategory.PortalID;
                tempCategory.Slug = postCategory.Slug;
                tempCategory.CategoryLink = Methods.GetPathExtension(_settings, _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/") + "/Category/" + postCategory.Slug);

                if (_categoriesShowPostCount)
                    tempCategory.Count = categories.Where(c => c.CategoryID == categoryID).Count();

                tempCategories.Add(tempCategory);
            }

            if (_categoriesSortedBy == "Category Name")
            {
                if (_categoriesSortOrder == "Ascending")
                    _categories = tempCategories.OrderBy(c => c.Category).ToList<BlogCategory>();
                else
                    _categories = tempCategories.OrderByDescending(c => c.Category).ToList<BlogCategory>();
            }
            else if (_categoriesSortedBy == "Posts Count")
            {
                if (_categoriesSortOrder == "Ascending")
                    _categories = tempCategories.OrderBy(c => c.Count).ToList<BlogCategory>();
                else
                    _categories = tempCategories.OrderByDescending(c=> c.Count).ToList<BlogCategory>();
            }

        }
    }
}